package drone.basis.util ;

import java.net.MalformedURLException ;
import java.net.URI ;
import java.net.URL ;

public final class UrlUtil
{

	public final static String setParam (
			String url ,
			String paramName ,
			String paramValue
	)
	{
		int temp_index = url.indexOf ( "?" ) ;
		if ( temp_index != - 1 )
		{
			int param_index = url.indexOf ( paramName + "=" , temp_index + 1 ) ;
			if ( param_index != - 1 )
			{
				temp_index = url.indexOf ( "&" , param_index + paramName.length ( ) + 1 ) ;
				if ( temp_index != - 1 )
				{
					return url.substring ( 0 , param_index ) + paramName + "=" + paramValue + url.substring ( temp_index ) ;
				}
				return url.substring ( 0 , param_index ) + paramName + "=" + paramValue ;
			} else
			{
				if ( url.lastIndexOf ( "&" ) == url.length ( ) - 1 )
				{
					return url + paramName + "=" + paramValue ;
				}
				return url + "&" + paramName + "=" + paramValue ;
			}
		} else
		{
			return url + "?" + paramName + "=" + paramValue ;
		}
	}

	public static final String getParamValue (
			String url ,
			String paramName
	)
	{
		int temp_index = url.indexOf ( "?" ) ;
		if ( temp_index != - 1 )
		{
			int param_index = url.indexOf ( paramName + "=" , temp_index + 1 ) ;
			if ( param_index != - 1 )
			{
				temp_index = url.indexOf ( "&" , param_index + paramName.length ( ) + 1 ) ;
				if ( temp_index != - 1 )
				{
					return url.substring ( param_index + paramName.length ( ) + 1 , temp_index ) ;
				}
				return url.substring ( param_index + paramName.length ( ) + 1 ) ;
			} else
			{
				return null ;
			}
		} else
		{
			return null ;
		}
	}

	public static final String removeParam (
			String url ,
			String ... paramNames
	)
	{
		for ( String paramName : paramNames )
		{
			url = removeParam ( url , paramName ) ;
		}
		return url ;
	}

	public static final String removeParam (
			String url ,
			String paramName
	)
	{
		int temp_index = url.indexOf ( "?" ) ;
		if ( temp_index != - 1 )
		{
			int param_index = url.indexOf ( paramName + "=" , temp_index + 1 ) ;
			if ( param_index != - 1 )
			{
				temp_index = url.indexOf ( "&" , param_index + paramName.length ( ) + 1 ) ;
				if ( temp_index != - 1 )
				{
					return url.substring ( 0 , param_index ) + url.substring ( temp_index + 1 ) ;
				}
				return url.substring ( 0 , param_index - 1 ) ;

			} else
			{
				return url ;
			}
		} else
		{
			return url ;
		}
	}

	/**
	 * Example:
	 * 
	 * <pre>
	 * {@code
	* url: http://www.abc.com/adf/fff?tokden=dKkdf&b=2323&c => http://www.abc.com
	 *
	 */
	public static String getHost (
			String url
	)
	{
		try
		{
			URI uri = URI.create ( url ) ;
			URI res = new URI ( uri.getScheme ( ) , uri.getUserInfo ( ) , uri.getHost ( ) , uri.getPort ( ) , null , null , null ) ;
			return res.toString ( ) ;
		} catch ( Exception e )
		{
			return url ;
		}

	}

	public static String urlJoin (
			URL url ,
			String locationHeader
	)
	{
		try
		{
			if ( locationHeader.startsWith ( "http" ) )
			{
				return new URL ( locationHeader ).toString ( ) ;
			}
			return new URL ( url.getProtocol ( ) + "://" + url.getAuthority ( ) + locationHeader ).toString ( ) ;
		} catch ( MalformedURLException e )
		{
			return url.toString ( ) ;
		}

	}

	public static void main (
			String [ ] args
	)
	{
		String url = "http://www.abc.com:8080/adf/fff?tokden=dKkdf&b=2323&c" ;
		String s = setParam ( url , "token" , "lksjfje9f09e.we09jj3.f83jjklfds" ) ;
		System.out.println ( s ) ;

		String r = removeParam ( url , "token" ) ;
		System.out.println ( r ) ;

		String b = getHost ( url ) ;
		System.out.println ( b ) ;
	}
}
